package com.example.gouwu2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {

    private EditText etSou;
    private Button btnSearch;
    private TextView tvTitle;
    private ListView listView;
    private HomeAdapter adapter;
    private List<HomeItem> itemList;

    private FrameLayout navHome,  navCart, navMain;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        etSou = findViewById(R.id.home_sou);
        btnSearch = findViewById(R.id.home_anniu);
        tvTitle = findViewById(R.id.home_biao);
        listView = findViewById(R.id.list_view);

        navHome = findViewById(R.id.nav_home);
        navCart = findViewById(R.id.nav_cart);
        navMain = findViewById(R.id.nav_main);

        tvTitle.setText("AMUSE ONESELF EVERY DAY");

        initData();

        // 创建适配器并设置给ListView
        adapter = new HomeAdapter(this, itemList);
        listView.setAdapter(adapter);

        // 设置搜索按钮点击事件
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = etSou.getText().toString().trim();
                if (input.isEmpty()) {
                    Toast.makeText(HomeActivity.this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
                } else {
                    // 执行搜索逻辑
                    searchItems(input);
                }
            }
        });

        // 设置ListView的点击事件 ，点击跳转到详情页
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取点击的商品
                HomeItem item = itemList.get(position);

                // 跳转到详情页
                Intent intent = new Intent(HomeActivity.this, HomeActivity2.class);
                intent.putExtra("title", item.getTitle());
                intent.putExtra("price", item.getPrice());
                intent.putExtra("imageRes", item.getImageRes());
                startActivity(intent);
            }
        });

        // 设置底部导航点击事件，可以跳转到不同Activity
        navHome.setOnClickListener(v -> {
            // 如果已经在HomeActivity，则不需要跳转
            if (!(getApplicationContext() instanceof HomeActivity)) {
                startActivity(new Intent(HomeActivity.this, HomeActivity.class));
            }
        });

        navCart.setOnClickListener(v -> {
            startActivity(new Intent(HomeActivity.this, CartActivity.class));
        });

        navMain.setOnClickListener(v -> {
            startActivity(new Intent(HomeActivity.this, MainActivity.class)); // navMain跳转到MainActivity
        });
    }

    // 初始化数据
    private void initData() {
        itemList = new ArrayList<>();
        // 添加示例数据
        itemList.add(new HomeItem("URBAN REVIVO2025春季新款女士青新碎绿果香调香水UAWP50003", "499.00", R.drawable.d));
        itemList.add(new HomeItem("UR2025夏季新款女装格雷系蝴蝶结装饰无袖连衣裙UWG750079", "259.00", R.drawable.a));
        itemList.add(new HomeItem("UR2025夏季新款男装时尚休闲百搭松紧腰抽绳短裤UMM650013", "339.00", R.drawable.c));
        itemList.add(new HomeItem("UR秋季新款女装时尚休闲设计感复古风小小织针织连衣裙UWG930006", "179.00", R.drawable.b));
        itemList.add(new HomeItem("UR2025夏季新款女波嬉风气质系带打揽超短款连衣裙UWG750105", "239.00", R.drawable.e));
        // 添加更多数据...
    }

    // 搜索功能
    private void searchItems(String keyword) {
        // 实现搜索过滤
        List<HomeItem> filteredList = new ArrayList<>();
        for (HomeItem item : itemList) {
            if (item.getTitle().contains(keyword)) {
                filteredList.add(item);
            }
        }
        // 更新适配器
        adapter.updateData(filteredList);
    }
}